layui.define(['laypage','jquery'],function (exports) {
    var $ = layui.jquery;
    var laypage = layui.laypage;
    var pageObj = {
        page1 : function (ajaxUrl,rowsFunc,opts) {
            opts = $.extend({
                tableId:'mytable',
                pageId:'mypage',
                currPage:1,
                groups:5
            },opts);
            var tableObj = $('#'+opts.tableId);
            if(tableObj && $('#'+opts.pageId)){
                $(tableObj).empty();
                $.post(ajaxUrl,{curr:opts.currPage},function (data) {
                    $(tableObj).append(rowsFunc($(data.result)));
                    laypage({
                        cont: opts.pageId,
                        pages: data.pages,          //总页数
                        groups: opts.groups,        //页分组数
                        skin: '#AF0000',
                        curr: opts.currPage || 1,   //当前页,
                        jump: function (obj, first) {//触发分页后的回调
                            if (!first) {//点击跳页触发函数自身，并传递当前页：obj.curr
                                opts.currPage = obj.curr;
                                pageObj.page1(ajaxUrl,rowsFunc,opts);
                            }
                        }
                    })
                })
            }
        },
        page2 : function (ajaxUrl,wrapRows,tableId,pageId,currPage,groups) {
            tableId = tableId || 'mytable';
            pageId = pageId || 'mypage';
            currPage = currPage || 1;
            groups = groups || 5;
            var tableObj = $('#'+tableId);
            if(tableObj && $('#'+pageId)){
                $(tableObj).empty();
                $.post(ajaxUrl,{curr:currPage},function (data) {
                    $(tableObj).append(wrapRows($(data.result)));
                    laypage({
                        cont: pageId,
                        pages: data.pages, //总页数
                        groups: groups,
                        skin: '#AF0000',
                        curr: currPage || 1, //当前页,
                        jump: function (obj, first) {//触发分页后的回调
                            if (!first) {//点击跳页触发函数自身，并传递当前页：obj.curr
                                pageObj.page2(ajaxUrl,wrapRows,tableId,pageId,obj.curr,groups);
                            }
                        }
                    })
                })
            }

        }
    }
    exports('mypage',pageObj);
})